package com.ynwt.service;

import com.ynwt.domain.User;
import com.ynwt.domain.UserEvent;
import com.ynwt.constant.EventType;
import com.ynwt.constant.JmsDestinationName;
import com.ynwt.constant.YcoinRankType;
import com.ynwt.jms.QueueClient;
import com.ynwt.model.ycoin.YcoinRankRequest;
import com.ynwt.model.ycoin.YcoinRankResponse;
import com.ynwt.model.front.SignRankBean;
import com.ynwt.model.front.SignRankItem;
import com.ynwt.repository.UserDao;
import com.ynwt.repository.UserEventDao;
import com.ynwt.util.JsonUtil;
import com.ynwt.util.RankUtil;
import com.ynwt.util.TimeUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.jms.JMSException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by huangying on 2017/5/24.
 */
@Service
public class RankServiceImpl implements RankService {

    private static final Logger LOG = LoggerFactory.getLogger(RankServiceImpl.class);

    @Override
    public YcoinRankResponse weekYcoinRank(String openid, Long page, Long pageSize) {

        User user = userDao.findOne(openid);

        //设置请求参数
        YcoinRankRequest request = new YcoinRankRequest();
        request.setUnionid(user.getUnionid());
        request.setPage(page);
        request.setPageSize(pageSize);
//        request.setType(YcoinRankType.PLACE);

        //向逸贝系统发送请求
        YcoinRankResponse rankResponse = null;
        try {
            rankResponse = JsonUtil.toBean(queueClient.sendAndReceive(request.toString(), JmsDestinationName.WEEK_YCOIN_RANK, 20000), YcoinRankResponse.class);
        } catch (JMSException e) {
            LOG.warn("method: weekYcoinRank. msg: weekYcoinRank error.", e);
        }

//        System.out.println(rankResponse.toString());

        //输出结果
        if (rankResponse.isSuccess()) {
            rankResponse.setRankItems(RankUtil.addPlaceAllowSamePlace(rankResponse.getRankItems(), 100));
            return rankResponse;
//            System.out.println(rankResponse.toString());
        }

        return null;
    }

    @Override
    public YcoinRankResponse totalYcoinRank(String openid, Long page, Long pageSize) {

        User user = userDao.findOne(openid);

        //设置请求参数
        YcoinRankRequest request = new YcoinRankRequest();
        request.setUnionid(user.getUnionid());
        request.setPage(page);
        request.setPageSize(pageSize);
//        request.setType(YcoinRankType.PLACE);

        //向逸贝系统发送请求
        YcoinRankResponse rankResponse = null;
        try {
            rankResponse = JsonUtil.toBean(queueClient.sendAndReceive(request.toString(), JmsDestinationName.TOTAL_YCOIN_RANK, 20000), YcoinRankResponse.class);
        } catch (JMSException e) {
            LOG.warn("method: totalYcoinRank. msg: totalYcoinRank error.", e);
        }

//        System.out.println(rankResponse.toString());

        //添加名次
        if (rankResponse.isSuccess()) {
            rankResponse.setRankItems(RankUtil.addPlaceAllowSamePlace(rankResponse.getRankItems(), 100));
            return rankResponse;
        }

        return null;
    }

    /**
     * 今日签到排行榜
     * @param page
     * @param pageSize
     */
    @Override
    public SignRankBean todaySignRank(Integer page, Integer pageSize) {

        //分页查询数据库数据
        Pageable pageable = new PageRequest(page, pageSize, Sort.Direction.ASC, "date");
        Page<UserEvent> signPage = userEventDao.findByDateGreaterThanAndEventKeyStartingWith(TimeUtil.startOfDateBaseNow(0), EventType.PREFIX_SIGN, pageable);
        SignRankBean rankBean = new SignRankBean();

        //设置总数
        rankBean.setTotal(signPage.getTotalElements());

        //设置列表
        List<UserEvent> list = signPage.getContent();
        List<SignRankItem> signRankItemList = new ArrayList<>();
        for (UserEvent userEvent : list) {
            SignRankItem item = new SignRankItem();
            User user = userEvent.getUser();
            item.setSignTime(userEvent.getDate().getTime());
            item.setUnionid(user.getUnionid());
            item.setHeadImgUrl(user.getHeadImgUrl());
            item.setNickname(user.getNickname());
            signRankItemList.add(item);
        }
        rankBean.setRankItems(signRankItemList);

        return rankBean;
    }


    @Autowired
    private QueueClient queueClient;

    @Autowired
    private UserDao userDao;

    @Autowired
    private UserEventDao userEventDao;
}
